package com.Maykk.Demo09;

/**
 * @ClassName: Box
 * @Description: TODO 奶箱类
 * @Author: MaykkStar@163.com
 */
public class Box {
    // 牛奶数量
    private int milk;
    private boolean state = false;

    // 生产者放入牛奶
    public synchronized void put(int milk) {
        if (state) {
            try {
                wait();
                System.out.println("箱子里还有牛奶，等一会再放");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        this.state = true;
        this.milk = milk;
        System.out.println(Thread.currentThread().getName() + "放入了第 " + milk + "瓶牛奶");
        notifyAll();
    }

    public synchronized void get() {
        if (!state) {
            try {
                System.out.println("箱子里还没有奶，再等一等");
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(Thread.currentThread().getName() + "取走了第 " + milk + "瓶奶");
        state = false;
        notifyAll();
    }
}
